package com.ruoyi.coal.modules.coalpile.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.Query;
import com.ruoyi.coal.modules.coalpile.domain.CoalPileInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardHistoryEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.PointEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalPileInfoVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.PdfVo;
import com.ruoyi.coal.modules.coalpile.mapper.CoalPileInfoDao;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardInfoDao;
import com.ruoyi.coal.modules.coalpile.service.CoalPileInfoService;
import com.ruoyi.coal.modules.coalpile.service.CoalYardHistoryService;
import com.ruoyi.coal.modules.coalpile.service.PointService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("coalPileInfoService")
//@DataSource(value = DataSourceType.SLAVE)
public class CoalPileInfoServiceImpl extends ServiceImpl<CoalPileInfoDao, CoalPileInfoEntity> implements CoalPileInfoService {
    @Autowired
    CoalYardHistoryService coalYardHistoryService;

    @Autowired
    CoalPileInfoService coalPileInfoService;

    @Autowired
    CoalPileInfoDao coalPileInfoDao;

    @Autowired
    PointService pointService;

    @Resource
    private CoalYardInfoDao coalYardInfoDao;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CoalPileInfoEntity> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty((String)params.get("key")))
            wrapper.eq("history_id", params.get("key"));
        if (StringUtils.isNotEmpty((String)params.get("pileKey")))
            wrapper.like("name", params.get("pileKey"));
        IPage<CoalPileInfoEntity> page = page((new Query())
                .getPage(params), (Wrapper)wrapper);
        return new PageUtils(page);
    }

    public List<CoalPileInfoVo> getALLByCoalYardId(Integer coalYardId) {
        QueryWrapper<CoalYardHistoryEntity> CoalYardWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)CoalYardWrapper.eq("coal_yard_id", coalYardId)).orderBy(true, false, (Object[])new String[] { "time" })).eq("measure_type", Integer.valueOf(1));
        List<CoalYardHistoryEntity> coalYardHistoryEntities = this.coalYardHistoryService.list((Wrapper)CoalYardWrapper);
        if (coalYardHistoryEntities.size() != 0) {
            Integer yardHistoryId = ((CoalYardHistoryEntity)coalYardHistoryEntities.get(0)).getId();
            QueryWrapper<CoalPileInfoEntity> CoalPileWrapper = new QueryWrapper();
            ((QueryWrapper)CoalPileWrapper.eq("history_id", yardHistoryId)).eq("measure_type", Integer.valueOf(1));
            ArrayList<CoalPileInfoVo> coalPileInfoVolist = new ArrayList<>();
            List<CoalPileInfoEntity> list = this.coalPileInfoService.list((Wrapper)CoalPileWrapper);
            for (CoalPileInfoEntity entity : list) {
                CoalPileInfoVo coalPileInfoVo = new CoalPileInfoVo();
                BeanUtils.copyProperties(entity, coalPileInfoVo);
                coalPileInfoVo.setMaxPoint((PointEntity)this.pointService.getById(entity.getMaxPoint()));
                coalPileInfoVo.setMinPoint((PointEntity)this.pointService.getById(entity.getMinPoint()));
                coalPileInfoVo.setUrlPcd(((CoalYardHistoryEntity)coalYardHistoryEntities.get(0)).getUrl());
                coalPileInfoVo.setNoColorGltf(entity.getUrlGltf2());
                coalPileInfoVolist.add(coalPileInfoVo);
            }
            return coalPileInfoVolist;
        }
        return new ArrayList<>();
    }

    public CoalPileInfoVo getOneByCoalYardIdAndCoalPileId(Integer coalYardId, Integer coalPileId) {
        QueryWrapper<CoalYardHistoryEntity> CoalYardWrapper = new QueryWrapper();
        ((QueryWrapper)CoalYardWrapper.eq("coal_yard_id", coalYardId)).orderBy(true, false, (Object[])new String[] { "time" });
        List<CoalYardHistoryEntity> coalYardHistoryEntities = this.coalYardHistoryService.list((Wrapper)CoalYardWrapper);
        if (coalYardHistoryEntities.size() != 0) {
            Integer yardHistoryId = ((CoalYardHistoryEntity)coalYardHistoryEntities.get(0)).getId();
            QueryWrapper<CoalPileInfoEntity> CoalPileWrapper = new QueryWrapper();
            QueryWrapper<CoalPileInfoEntity> eq = (QueryWrapper<CoalPileInfoEntity>)((QueryWrapper)CoalPileWrapper.eq("history_id", yardHistoryId)).eq("id", coalPileId);
            CoalPileInfoEntity one = (CoalPileInfoEntity)this.coalPileInfoService.getOne((Wrapper)eq);
            if (one != null) {
                CoalPileInfoVo coalPileInfoVo = new CoalPileInfoVo();
                BeanUtils.copyProperties(one, coalPileInfoVo);
                return coalPileInfoVo;
            }
        }
        return new CoalPileInfoVo();
    }

    public List<CoalPileInfoVo> getALLByHistoryId(Integer historyId) {
        QueryWrapper<CoalPileInfoEntity> CoalPileWrapper = new QueryWrapper();
        CoalPileWrapper.eq("history_id", historyId);
//        List list = this.coalPileInfoService.list((Wrapper) CoalPileWrapper);
        return (List<CoalPileInfoVo>)this.coalPileInfoService.list((Wrapper)CoalPileWrapper).stream().map(entity -> {
            CoalPileInfoVo coalPileInfoVo = new CoalPileInfoVo();
            BeanUtils.copyProperties(entity, coalPileInfoVo);
//            PointEntity byId = this.pointService.getById(((CoalPileInfoEntity) entity).getMaxPoint());
            coalPileInfoVo.setMaxPoint((PointEntity)this.pointService.getById(((CoalPileInfoEntity) entity).getMaxPoint()));
            coalPileInfoVo.setMinPoint((PointEntity)this.pointService.getById(((CoalPileInfoEntity) entity).getMinPoint()));
            return coalPileInfoVo;
        }).collect(Collectors.toList());
    }

    public PdfVo getPdfVoByPileId(int id) {
        return this.coalPileInfoDao.getPdfVoByPileId(Integer.valueOf(id));
    }

    public List<CoalPileInfoEntity> getCutUpMeasureFile(Integer cutUpId) {
        List<CoalPileInfoEntity> coalPileInfoEntities = this.coalPileInfoDao.selectList((Wrapper)(new QueryWrapper()).eq("cut_id", cutUpId));
        return coalPileInfoEntities;
    }

    public List<CoalPileInfoVo> getCoalPileExceptMain(Integer coalYardId) {
        List<CoalPileInfoVo> coalPileInfoVos = this.coalPileInfoService.getALLByCoalYardId(coalYardId);
        CoalYardInfoEntity coalYardInfoEntity = (CoalYardInfoEntity)this.coalYardInfoDao.selectById(coalYardId);
        for (CoalPileInfoVo coalPileInfoVo : coalPileInfoVos) {
            if (coalPileInfoVo.getName().equals(coalYardInfoEntity.getName()))
                coalPileInfoVos.remove(coalPileInfoVo);
        }
        return coalPileInfoVos;
    }

    public List<CoalPileInfoVo> getCoalPileGltf(Integer coalYardId) {
        List<CoalPileInfoVo> coalPileInfoVos = getALLByCoalYardId(coalYardId);
        ArrayList<CoalPileInfoVo> coalPileInfoGltfList = new ArrayList<>();
        if (coalPileInfoVos.size() == 0)
            return new ArrayList<>();
        if (coalPileInfoVos.size() == 1 && ((CoalPileInfoVo)coalPileInfoVos.get(0)).getName().equals(((CoalYardInfoEntity)this.coalYardInfoDao.selectById(coalYardId)).getName()))
            return coalPileInfoVos;
        for (CoalPileInfoVo coalPileInfoVo : coalPileInfoVos) {
            if (!coalPileInfoVo.getName().equals(((CoalYardInfoEntity)this.coalYardInfoDao.selectById(coalYardId)).getName()))
                coalPileInfoGltfList.add(coalPileInfoVo);
        }
        return coalPileInfoGltfList;
    }
}

